home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / GRAHAM / XA_6S.ZIP / SOURCE / APPL_WRT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-01  |  2.6 KB  |  96 lines

  1. /*
  2.  * XaAES - XaAES Ain't the AES
  3.  *
  4.  * A multitasking AES replacement for MiNT
  5.  *
  6.  */
  7.  
  8. #include <MINTBIND.H>
  9. #include <OSBIND.H>
  10. #include <memory.h>
  11. #include "XA_DEFS.H"
  12. #include "XA_TYPES.H"
  13. #include "XA_GLOBL.H"
  14. #include "K_DEFS.H"
  15. #include "EVNT_MUL.H"
  16.  
  17. /*
  18.     XaAES's current appl_write() only works for standard 16 byte messages
  19. */
  20. unsigned long XA_appl_write(short clnt_pid, AESPB *pb)
  21. {
  22.     XA_AESMSG_LIST *new_msg,*ml;
  23.     XA_CLIENT *dest_client;
  24.     char *clnt_buf;
  25.     unsigned long retv=XA_OK;
  26.     short dest_id=pb->intin[0];
  27.     short msg_len=pb->intin[1],f;
  28.     char *msg=(char*)pb->addrin[0];
  29.  
  30.     DIAGS(("appl_write():dest=%d,msg_len=%d,msg=%lx\n",dest_id,msg_len,msg));
  31.     
  32.     pb->intout[1]=0;
  33.     
  34.     if (dest_id==AESpid)     /* Just a precaution - we don't want to send messages to ourselves..... */
  35.         return XAC_DONE;
  36.  
  37.     if (msg_len>16)
  38.         return XAC_DONE;
  39.  
  40.     dest_client=Pid2Client(dest_id);
  41.  
  42.     if (!dest_client)
  43.     {
  44.         DIAGS(("WARNING: Invalid target pid [%d] for appl_write()\n",dest_id));
  45.         return XAC_DONE;
  46.     }
  47.     
  48.     Psemaphore(2,CLIENTS_SEMAPHORE,-1L);
  49.     
  50.     if (dest_client->waiting_for&XAWAIT_MESSAGE)    /* Is the dest client waiting for a message at the moment? */
  51.     {
  52.         if (dest_client->waiting_for&XAWAIT_MULTI)    /* If the client is waiting on a multi, the response is  */
  53.         {                                                /* slightly different to the evnt_mesag() response. */
  54.             dest_client->waiting_pb->intout[0]=MU_MESAG;
  55.             cancel_evnt_multi(dest_id);
  56.         }else{
  57.             dest_client->waiting_pb->intout[0]=1;
  58.             dest_client->waiting_for=0;    /* flag client as not waiting for anything */
  59.         }
  60.         
  61.         clnt_buf=(char*)(dest_client->waiting_pb->addrin[0]);
  62.         for(f=0; f<msg_len; f++)
  63.             clnt_buf[f]=msg[f];            /* Fill in the clients message buffer */
  64.  
  65.         Fwrite(dest_client->clnt_pipe_wr, (long)sizeof(unsigned long),&retv);    /* Write success to clients reply pipe to unblock the process */
  66.     }else{    /* Create a new entry in the destination client's pending messages list */
  67.         new_msg=(XA_AESMSG_LIST*)malloc(sizeof(XA_AESMSG_LIST));
  68.         
  69.         if (new_msg)
  70.         {
  71.             pb->intout[1]=1;
  72.  
  73.             clnt_buf=(char*)new_msg->message;
  74.             
  75.             for(f=0; f<msg_len; f++)
  76.                 clnt_buf[f]=msg[f];            /* Fill in the new pending list entry with the message */
  77.         
  78.             new_msg->next=NULL;
  79.  
  80.             if (dest_client->msg)    /* There are already some pending messages */
  81.             {
  82.                 for(ml=dest_client->msg; ml->next; ml=ml->next);
  83.                 ml->next=new_msg;    /* Append the new message to the list */
  84.             }else{                    /* First entry in the clients pending message list */
  85.                 dest_client->msg=new_msg;
  86.             }
  87.  
  88.         }
  89.     }
  90.     
  91.     Psemaphore(3,CLIENTS_SEMAPHORE,0L);
  92.     
  93.     return XAC_DONE;
  94.  
  95. }
  96.